#! /bin/bash

if [ "${1#-}" != "$1" ]; then
    echo """
    Usage:
    `basename $0`  METER TEMPO COUNT
       defaults:   4/4   120   20

       Ex.: `basename $0` 6/8 90
    """
    exit 1
fi

bate(){
    local meter=${1:-4}
    local sleepfor=${2:-0.5}
    local count=${3:-20}

    [ $count -eq 0 ] && return

    # what would be of the world without for?
    for i in `seq 1 $meter`
    do
        str="."
        [ $i -eq 1 ] && str="   #"

        # uncomment for beep
        # str="$str"'\a'

        echo -ne "$str"
        sleep $sleepfor
    done

    [ $((count % 8)) -eq 0 ] && echo

    # everybody loves tail recursion!
    bate $meter $sleepfor $((count - 1))
}

# arguments:
meter=${1:-"4/4"}
bpm=${2:-120}
count=${3:-20}

h_meter=`echo $meter | sed 's/^\([0-9]\)\+[^0-9]\([0-9]\+\)/\1/'`
l_meter=`echo $meter | sed 's/^\([0-9]\)\+[^0-9]\([0-9]\+\)/\2/'`

sleepfor=`echo -e "scale=5\n240/($l_meter*$bpm)" | bc`

bate $h_meter $sleepfor $count

echo
exit 0
